Imports System.Windows.Forms

Public Class frmTracking

    Public Enum enumTrackingMode
        Create
        Edit
        Validate
        Transfer
        Import
    End Enum
    Private da As DbDataAdapter

    Private Mode As enumTrackingMode
    Private ThisRow As DataRow
    Private WorkingTable, ImportTable As String

    ''' <summary>
    ''' Instantiate form
    ''' </summary>
    ''' <param name="TrackingMode">Controls the form's display mode</param>
    ''' <param name="WorkingName">Name of existing Working Table (blank for new)</param>
    ''' <param name="ImportName">If mode is Import, pass name of file that was imported</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal TrackingMode As enumTrackingMode, ByVal WorkingName As String, Optional ByVal ImportName As String = "")
        InitializeComponent()
        WorkingTable = WorkingName
        ImportTable = ImportName
        Mode = TrackingMode
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Close()
    End Sub

    Private Sub btnHelp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHelp.Click
        ShowHelp(Me, "13")
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        With Project
            'make sure null project_name fields have values; delete rows that already exist
            If ThisRow.Table.Columns.Contains("Project_Name") Then
                For r As Integer = ThisRow.Table.Rows.Count - 1 To 0 Step -1
                    Dim row As DataRow = ThisRow.Table.Rows(r)
                    If IsDBNull(row.Item("Project_Name")) Then row.Item("Project_Name") = .Name
                Next
                ThisRow.Item("Project_Name") = .Name
            End If
            WorkingTable = txtTableName.Text
            ThisRow.Item("Temp_DB_Name") = WorkingTable
            If Mode = enumTrackingMode.Import Then ThisRow.Item("Master_DB_Name") = ImportTable
            ThisRow.Item("Description") = txtDescription.Text
            ThisRow.Item("Source") = txtSource.Text
            ThisRow.Item("QCode") = .DB.GetPickList(cboQCode)
            ThisRow.Item("Q_Info") = txtAssessment.Text
            ThisRow.Item("Q_Info") = txtAssessment.Text
            ThisRow.Item("TableGroup") = cboTableGroup.Text
            ThisRow.Item("Create_Date") = IIf(IsDate(txtCreatedDate.Text), txtCreatedDate.Text, DBNull.Value)
            ThisRow.Item("Create_Init") = txtCreatedInit.Text
            ThisRow.Item("Validate_Date") = IIf(IsDate(txtValidatedDate.Text), txtValidatedDate.Text, DBNull.Value)
            ThisRow.Item("Validate_Init") = txtValidatedInit.Text
            ThisRow.Item("Transfer_Date") = IIf(IsDate(txtTransferredDate.Text), txtTransferredDate.Text, DBNull.Value)
            ThisRow.Item("Transfer_Init") = txtTransferredInit.Text
            ThisRow.Item("Is_Import") = CBool(Mode = enumTrackingMode.Import)
            ThisRow.Item("Create_Update") = Now
            .ResetPDox(clsProject.enumTableTypes.Tracking)
            Try
                da.Update(Project.DataSet.Tables("Tracking"))
                .DataSet.Tables("Tracking").AcceptChanges()
            Catch ex As Exception
                'ErrorMsg("An error occurred when trying to update the Support table: Tracking", ex)
            End Try
            DialogResult = Windows.Forms.DialogResult.OK
            Close()
        End With
    End Sub

    Private Sub frmTracking_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        If chkHideForm.Checked Then MainWRDB.PromptImport = False
        SaveWindowPos(REGAPP, Me)
    End Sub

    Private Sub frmTracking_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            GetWindowPos(REGAPP, Me)
            txtTableName.Text = WorkingTable
            txtTableName.MaxLength = Project.dsWRDB.Tables("TRACKING").Columns("TEMP_DB_NAME").MaxLength
            Select Case Mode
                Case enumTrackingMode.Create : Text = "New Working Table"
                Case enumTrackingMode.Edit : Text = "Edit Working Table"
                Case enumTrackingMode.Import : Text = "Track Imports to Working Table (Cancel to Bypass)"
                Case enumTrackingMode.Validate : Text = "Validate Working Table"
                Case enumTrackingMode.Transfer : Text = "Transfer Working Table"
            End Select

            If Not Project.ResetPDox("Tracking") Then Exit Sub
            da = Project.DB.AddTable(Project.DataSet, "Tracking", "Select * from Tracking WHERE Is_Import=" & IIf(Mode = enumTrackingMode.Import, 1, 0))
            If da Is Nothing Then Exit Sub
            ThisRow = Nothing
            With Project.DataSet.Tables("Tracking")
                'try to find an existing record matching working and master table
                .CaseSensitive = False
                Dim rTbl() As DataRow = .Select(String.Format("Temp_DB_Name='{0}' AND Master_DB_Name='{1}'", WorkingTable, Project.MasterTableName))
                If rTbl.Length = 1 Then
                    ThisRow = rTbl(0)
                Else 'if can't find, just select first matching working table entry
                    rTbl = .Select(String.Format("Temp_DB_Name='{0}'", WorkingTable))
                    If rTbl.Length > 0 Then ThisRow = rTbl(0)
                End If
                'For Each r As DataRow In .Rows
                '    If r.Item("Temp_DB_Name").ToString.ToUpper = WorkingTable.ToUpper Then
                '        If r.Item("Master_DB_Name").ToString.ToUpper = IIf(Mode = enumTrackingMode.Import, ImportTable.ToUpper, Project.MasterTableName.ToUpper) Then
                '            ThisRow = r
                '            Exit For
                '        End If
                '    End If
                'Next
                If ThisRow Is Nothing Then
                    ThisRow = .Rows.Add
                    ThisRow.Item("Temp_Db_Name") = WorkingTable
                    ThisRow.Item("Master_DB_Name") = IIf(Mode = enumTrackingMode.Import, ImportTable.ToUpper, Project.MasterTableName.ToUpper)
                    If .Columns.Contains("Project_Name") Then
                        ThisRow.Item("Project_Name") = Project.Name
                    End If
                End If
            End With
            txtTableName.Text = ThisRow.Item("Temp_Db_Name").ToString
            txtDescription.Text = ThisRow.Item("Description").ToString
            txtSource.Text = ThisRow.Item("Source").ToString
            Project.ResetPDox(clsProject.enumTableTypes.QCodes)
            Project.DB.FillPickList(cboQCode, "", "QCodes", "QCode", "QCode_Name", , ThisRow.Item("QCode").ToString)
            txtAssessment.Text = ThisRow.Item("Q_Info").ToString
            Project.DB.FillPickList(cboTableGroup, "", "Tracking", "TableGroup", "")
            cboTableGroup.Text = ThisRow.Item("TableGroup").ToString
            If Not IsDBNull(ThisRow.Item("Create_Date")) Then txtCreatedDate.Text = CDate(ThisRow.Item("Create_Date")).ToString(MyDateTimeFormat)
            txtCreatedInit.Text = ThisRow.Item("Create_Init").ToString
            If Not IsDBNull(ThisRow.Item("Validate_Date")) Then txtValidatedDate.Text = CDate(ThisRow.Item("Validate_Date")).ToString(MyDateTimeFormat)
            txtValidatedInit.Text = ThisRow.Item("Validate_Init").ToString
            If Not IsDBNull(ThisRow.Item("Transfer_Date")) Then txtTransferredDate.Text = CDate(ThisRow.Item("Transfer_Date")).ToString(MyDateTimeFormat)
            txtTransferredInit.Text = ThisRow.Item("Transfer_Init").ToString

            txtTableName.Enabled = Mode = enumTrackingMode.Create

            Select Case Mode
                Case enumTrackingMode.Create, enumTrackingMode.Import
                    txtCreatedDate.Text = Now.ToString(MyDateTimeFormat)
                    txtCreatedInit.Text = MainWRDB.UserName
                    txtValidatedDate.Enabled = False
                    txtValidatedInit.Enabled = False
                    txtTransferredDate.Enabled = False
                    txtTransferredInit.Enabled = False
                    chkHideForm.Visible = Mode = enumTrackingMode.Import
                Case enumTrackingMode.Validate
                    txtValidatedDate.Text = Now.ToString(MyDateTimeFormat)
                    txtValidatedInit.Text = MainWRDB.UserName
                    txtTransferredDate.Enabled = False
                    txtTransferredInit.Enabled = False
                Case enumTrackingMode.Transfer
                    txtTransferredDate.Text = Now.ToString(MyDateTimeFormat)
                    txtTransferredInit.Text = MainWRDB.UserName
            End Select
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub txtTableName_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtTableName.Validating
        If Mode <> enumTrackingMode.Create Then Exit Sub
        Dim TableName As String = Project.DB.GetValidTableName(txtTableName.Text)
        If TableName = "" Then e.Cancel = True : Exit Sub
        Project.ResetPDox(TableName)
        If Project.DB.TableExists(TableName) Then
            If WarningMsg("This table already exists; do you want to overwrite it?") = Windows.Forms.DialogResult.OK Then
                Project.DB.DropTable(TableName)
            Else
                e.Cancel = True
                Exit Sub
            End If
        End If
        txtTableName.Text = TableName
    End Sub
End Class
